package study.数据库;

import cn.hutool.db.Db;
import cn.hutool.db.DbUtil;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.ds.simple.SimpleDataSource;
import cn.hutool.db.handler.EntityListHandler;
import cn.hutool.db.sql.SqlExecutor;
import cn.hutool.log.Log;
import cn.hutool.setting.Setting;
import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
//import study.entity.User;
import study.User;
import study.entity.common.EntityVo;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author 刘伟涛
 * @Date 2024/7/2 14:40
 **/
@Slf4j
public class Demo01 {
    public static void main(String[] args) {
        Connection conn = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver"); // MySQL 驱动
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try {
            /*// 方法一
            String tableName = "user";
            String url = ("jdbc:mysql://localhost:3306/ucm-dev?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&rewriteBatchedStatements=true&serverTimezone=Asia/Shanghai");
            conn = DriverManager.getConnection(url, "root", "root");
//             执行非查询语句，返回影响的行数
            int count = SqlExecutor.execute(conn, "UPDATE " + tableName + " set field1 = ? where id = ?", 0, 0);
            log.info("影响行数：{}", count);
//             执行非查询语句，返回自增的键，如果有多个自增键，只返回第一个
            Long generatedKey = SqlExecutor.executeForGeneratedKey(conn, "UPDATE " + tableName + " set field1 = ? where id = ?", 0, 0);
            log.info("主键：{}", generatedKey);

            *//* 执行查询语句，返回实体列表，一个Entity对象表示一行的数据，Entity对象是一个继承自HashMap的对象，存储的key为字段名，value为字段值 *//*
            List<Entity> entityList = SqlExecutor.query(conn, "select * from " + tableName, new EntityListHandler());
            log.info("{}", entityList);*/


            //方法二
            SimpleDataSource ds = new SimpleDataSource("jdbc:mysql://localhost:3306/ucm-dev", "root", "root");
            // 查询
            List<Entity> all = Db.use(ds).findAll(Entity.create("user").set("id", "04ee3bea708349879b03dbdf6766af11"));
            // 修改update（修改字段,条件字段）
            int update = Db.use(ds).update(Entity.create().set("address", "study"),Entity.create("user").set("id", "04ee3bea708349879b03dbdf6766af11"));
            log.info("update:{}",update);
            // 增加
            int insert = Db.use(ds).insert(Entity.create("user").set("id", "04ee3bea708349879b03dbdf6766af22").set("area_code", "86"));
            log.info("insert:{}",insert);

            // 映射结果到 User 对象
            List<User> users = new ArrayList<>();
            for (Entity entity : all) {
                String id = entity.getStr("id");
                String address = entity.getStr("address");
                Date birthday = entity.getDate("birthday");
                Date cjsj = entity.getDate("cjsj");
                String email = entity.getStr("email");
                String identity = entity.getStr("identity");
                Date lastLogin = entity.getDate("last_login");
                Date lastResetPasswd = entity.getDate("last_reset_passwd");
                String name = entity.getStr("name");
                String password = entity.getStr("password");
                String phone = entity.getStr("phone");
                String qq = entity.getStr("qq");
                Integer sex = entity.getInt("sex");
                String username = entity.getStr("username");
                String wechat = entity.getStr("wechat");
                Date xgsj = entity.getDate("xgsj");
                Integer zt = entity.getInt("zt");
                String areaCode = entity.getStr("area_code");

//                User user = new User(id, address, birthday, cjsj, email, identity, lastLogin, lastResetPasswd, name, password, phone, qq, sex, username, wechat, xgsj, zt, areaCode);
//                users.add(user);
            }

            // 输出用户列表
            for (User user : users) {
                System.out.println(user);
            }


            log.info("all{}", all);
        } catch (SQLException e) {
            log.error("SQL error!{}",e);
        } finally {
            DbUtil.close(conn);
        }

    }
}
